package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.BRD_POW;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.CPU;
import cn.edu.nju.software.cdiy.bean.Disk;
import cn.edu.nju.software.cdiy.bean.Driver;
import cn.edu.nju.software.cdiy.bean.Graphics;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.Interface;
import cn.edu.nju.software.cdiy.bean.MainBoard;
import cn.edu.nju.software.cdiy.bean.Memory;
import cn.edu.nju.software.cdiy.bean.NetworkAdapter;
import cn.edu.nju.software.cdiy.bean.POW_Interface;
import cn.edu.nju.software.cdiy.bean.Power;
import cn.edu.nju.software.cdiy.bean.SoundCard;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.PowerDao;
import cn.edu.nju.software.cdiy.util.Changer;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.FilterItem;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.Pager;
import cn.edu.nju.software.cdiy.util.SortByHotAsc;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class PowerDaoImpl implements PowerDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Power> getPowerList(int page, SortType s, HardwareSet hs,
			SortWay sw) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<Power> list = null;

		// 第page页的第minNum条记录到maxNum条记录
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		switch (s) {
		case PRICE:
			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM Power WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM Power WHERE STATE=882 ORDER BY OPRICE DESC";
			list = sess.createQuery(sql).list();
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Power WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM Power WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			list = sess.createQuery(sql).list();
			break;
		case W_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Power WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM Power WHERE STATE=882 ORDER BY WSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case Z_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Power WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM Power WHERE STATE=882 ORDER BY ZSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case NEW:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Power WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM Power WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			list = sess.createQuery(sql).list();

			break;
		case HOT:
			sql = "FROM Power WHERE STATE=882";
			list = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(list);
			else {
				Collections.sort(list, new SortByHotAsc());
			}
			break;
		default:
			break;

		}
		list = filterList(hs, list, minNum, maxNum);
		
		return list;
	}

	private List<Power> filterList(HardwareSet hs, List<Power> list, int min,
			int max) {
		/*
		 * 分别由各部件的filter对待检测的部件表进行筛选 目前有若干个条件可以用来筛选 MainBoard CPU Graphics Disk
		 * Memory Drive SoundCard Networkadapter
		 */
		list = uniFilter(hs, list);
		// 最终根据页数的要求返回list
		List<Power> returnList = new Pager<Power>().getPageList(list, min, max);
		return returnList;
	}

	private List<Power> HsFilter(HardwareSet hs, List<Power> l) {
		/*
		 * HardwareSet对电源的限制
		 */
		int totalPower = 0;
		// 返回的List
		List<Power> returnList = new ArrayList<Power>();

		if (hs.getCpu() != null) {
			CPU cpu = hs.getCpu();
			int cpuPower = cpu.getRatedPower();
			totalPower = totalPower + cpuPower;
		}
		if (hs.getDisk() != null) {
			Disk disk = hs.getDisk();
			int diskPower = disk.getRatedPower();
			totalPower = totalPower + diskPower;
		}
		if (hs.getDriver() != null) {
			Driver dri = hs.getDriver();
			int driPower = dri.getRatedPower();
			totalPower = totalPower + driPower;
		}
		if (hs.getGraphics() != null) {
			Graphics Gra = hs.getGraphics();
			int graPower = Gra.getRatedPower();
			totalPower = totalPower + graPower;
		}
		if (hs.getMainBoard() != null) {
			MainBoard mb = hs.getMainBoard();
			int mbPower = mb.getRatedPower();
			totalPower = totalPower + mbPower;
		}
		if (hs.getMemory() != null) {
			Memory mem = hs.getMemory();
			int memPower = mem.getRatedPower();
			totalPower = totalPower + memPower;
		}
		if (hs.getNetworkAdapter() != null) {
			NetworkAdapter na = hs.getNetworkAdapter();
			int naPower = na.getRatedPower();
			totalPower = totalPower + naPower;
		}
		if (hs.getSoundCard() != null) {
			SoundCard sc = hs.getSoundCard();
			int scPower = sc.getRatedPower();
			totalPower = totalPower + scPower;
		}

		for (int i = 0; i < l.size(); i++) {
			Power nowPw = l.get(i);
			int pwRatedPower = nowPw.getRatedPower();
			if (pwRatedPower >= totalPower) {
				returnList.add(nowPw);
				break;
			}
		}
		return returnList;
	}

	@Override
	public List<Hardware> getNewestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Power WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
		List<Power> list = sess.createQuery(sql).list();
		if (list.size() > Constant.NEW_LIST_LENGTH)
			list = list.subList(0, Constant.NEW_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Hardware> getHottestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Power WHERE STATE=882";
		List<Power> list = sess.createQuery(sql).list();

		Collections.sort(list);

		if (list.size() > Constant.HOT_LIST_LENGTH)
			list = list.subList(0, Constant.HOT_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Brand> getBrandList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM BRD_POW";
		List<Brand> returnList = new ArrayList<Brand>();
		List<BRD_POW> list = sess.createQuery(sql).list();

		for (int i = 0; i < list.size(); i++) {
			returnList.add(list.get(i));
		}

		
		return returnList;
	}

	@Override
	public List<Power> filter(int page, Map<String, List<Integer>> map,
			SortType st, SortWay sw, HardwareSet hs) {
		// 搜索时的过滤方法
		if (map==null || map.size()==0)
			return getPowerList(page, st, hs, sw);

		// 获得此产品表
		List<Power> hwList = getFilterList(hs, st, sw);

		// 最后的匹配表
		List<Power> matchList = new ArrayList<Power>();

		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.powItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.powItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer
								.checkPrice(hwList.get(j),
										Constant.POWER_PRICE[list.get(k) - 1]
												.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.powItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer
								.checkPrice(hwList.get(j),
										Constant.POWER_RATED[list.get(k) - 1]
												.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.powItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getFanSize().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<Power>();
			}
		}
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);
		matchList = new Pager<Power>().getPageList(matchList, minNum, maxNum);
		return matchList;
	}

	private List<Power> getFilterList(HardwareSet hs, SortType st, SortWay sw) {
		/*
		 * 获取用于筛选的硬件列表 此列表可以由action提供 或由本方法从数据库获取
		 */
		List<Power> l = new ArrayList<Power>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;

		switch (st) {
		case PRICE:
			
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					sql = "FROM Power WHERE STATE=882 ORDER BY OPRICE ASC";
				else
					sql = "FROM Power WHERE STATE=882 ORDER BY OPRICE DESC";
				l = sess.createQuery(sql).list();
			
			break;
		case W_SCORE:
			
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Power WHERE STATE=882 ORDER BY WSCORE DESC";
				else
					sql = "FROM Power WHERE STATE=882 ORDER BY WSCORE ASC";
				l = sess.createQuery(sql).list();
			
			break;
		case Z_SCORE:
			
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Power WHERE STATE=882 ORDER BY ZSCORE DESC";
				else
					sql = "FROM Power WHERE STATE=882 ORDER BY ZSCORE ASC";
				l = sess.createQuery(sql).list();
			
			break;
		case NEW:
			
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Power WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
				else
					sql = "FROM Power WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
				l = sess.createQuery(sql).list();
			
			break;
		case HOT:
			
				sql = "FROM Power WHERE STATE=882";
				l = sess.createQuery(sql).list();
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					Collections.sort(l);
				else {
					Collections.sort(l, new SortByHotAsc());
				}
			
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Power WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM Power WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			l = sess.createQuery(sql).list();
			break;
		default:
			break;

		}
		
		l = uniFilter(hs, l);
		return l;
	}

	@Override
	public List<Power> search(String keywords, int page) {
		Session sess = sessionFactory.getCurrentSession();

		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		String sql = "FROM Power WHERE STATE=882";
		String keys[] = keywords.split(" ");
		for (int i = 0; i < keys.length; i++) {
			if (i == 0) {
				sql = sql + " WHERE MODEL LIKE '%" + keys[i] + "%'";
			} else {
				sql = sql + " OR MODEL LIKE '%" + keys[i] + "%'";
			}
		}
		List<Power> list = sess.createQuery(sql).list();

		List<Power> returnList = new Pager<Power>().getPageList(list, minNum,
				maxNum);

		
		return returnList;
	}

	@Override
	public HashMap<String, List<Item>> getFilterBoard() {
		HashMap<String, List<Item>> map = new HashMap<String, List<Item>>();
		map.put(getBrandItem().getItemName(), getFilterBrandItem());
		map.put(getPriceItem().getItemName(), getFilterPriceItem());

		for (int i = 2; i < Constant.powItemNameList.length; i++) {
			map.put(Constant.powItemNameList[i], getFilterItemByNum(i));
		}

		return map;
	}

	private List<Item> getFilterItemByNum(int i) {
		Session sess = sessionFactory.getCurrentSession();

		String sql = null;
		List<Value> values = null;
		List<Item> choices = null;

		switch (i) {
		case 2:
			// 额定功率

			// itemChoiceList
			choices = new ArrayList<Item>();
			for (int j = 0; j < (Constant.POWER_RATED.length); j++) {
				choices.add(Constant.POWER_RATED[j]);
			}

			break;
		case 3:
			// 风扇尺寸

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=716 AND KEY<=725 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		default:
			break;

		}
		
		return choices;
	}

	private FilterItem getItemByNum(int i) {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		// itemName
		fi.setItemName(Constant.powItemNameList[i]);

		String sql = null;
		List<Value> values = null;
		List<String> choices = null;

		switch (i) {
		case 2:
			// 额定功率

			// itemChoiceList
			choices = new ArrayList<String>();
			for (int j = 0; j < (Constant.POWER_RATED.length); j++) {
				choices.add(Constant.POWER_RATED[j].getName());
			}

			break;
		case 3:
			// 风扇尺寸

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=716 AND KEY<=725 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		default:
			break;

		}
		fi.setItemChoiceList(choices);
		
		return fi;
	}

	private FilterItem getPriceItem() {
		// 价格项
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.powItemNameList[1]);

		List<String> choices = new ArrayList<String>();
		choices.add("150元以下");
		choices.add("150-200元");
		choices.add("200-300元");
		choices.add("300-600元");
		choices.add("600-1000元");
		choices.add("1000元以上");

		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getBrandItem() {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.powItemNameList[0]);

		String sql = "FROM BRD_POW";
		List<BRD_POW> brandList = sess.createQuery(sql).list();
		List<String> choices = new ArrayList<String>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(brandList.get(i).getBrand());
		}

		fi.setItemChoiceList(choices);
		
		return fi;
	}

	@Override
	public Power getOneById(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Power c = (Power) sess.load(Power.class, id);
		
		return c;
	}

	@Override
	public Map<String, List<String>> getInsertboard() {
		// 返回的map
		Map<String, List<String>> rMap = new HashMap<String, List<String>>();

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<String> l = null;
		List<Value> valueList = null;

		// 品牌
		sql = "FROM BRD_POW";
		l = new ArrayList<String>();
		List<BRD_POW> brandList = sess.createQuery(sql).list();
		for (int i = 0; i < brandList.size(); i++) {
			l.add(brandList.get(i).getBrand());
		}
		rMap.put(Constant.powSelectStr[0], l);

		// Value
		// 风扇尺寸
		rMap.put(Constant.powSelectStr[2], getValueByKey(716, 725));

		// PFC类型
		l = getValueByKey(714, 715);
		rMap.put(Constant.powSelectStr[1], l);

		// 出线类型
		l = getValueByKey(726, 730);
		rMap.put(Constant.powSelectStr[3], l);

		// 一对多
		// 电源接口
		l = new ArrayList<String>();
		sql = "FROM Interface WHERE RE_POW=1 ORDER BY INTF_TYPE,NAME";
		List<Interface> list3 = sess.createQuery(sql).list();
		for (int i = 0; i < list3.size(); i++) {
			l.add(list3.get(i).getName());
		}
		rMap.put(Constant.powComboBoxStr[0], l);

		
		return rMap;
	}

	@Override
	public boolean insertHardware(Map<String, List<String>> map) {
		try {
			// TODO Auto-generated method stub
			Session sess = sessionFactory.getCurrentSession();
			Power pow = new Power();
			// 本方法处理外键和专属属性，通用属性由其他方法提供
			// power有一对多的情况
			Iterator i = map.keySet().iterator();
			// 一对多的list
			List<String> powIntf = null;
			while (i.hasNext()) {
				String key = i.next().toString();
				List<String> list = map.get(key);
				String sql = null;
				if (key.equals(Constant.powInforStr[0])) {
					pow.setModel(list.get(0));
					// 外键
				} else if (key.equals(Constant.powSelectStr[0])) {
					sql = "FROM BRD_POW WHERE BRAND='" + list.get(0) + "'";
					List<BRD_POW> bl = sess.createQuery(sql).list();
					pow.setBrand(bl.get(0));
				} else if (key.equals(Constant.powSelectStr[1])) {
					pow.setPFC(getValueByName(list.get(0)));
				} else if (key.equals(Constant.powSelectStr[2])) {
					pow.setFanSize(getValueByName(list.get(0)));
				} else if (key.equals(Constant.powSelectStr[3])) {
					pow.setOutletType(getValueByName(list.get(0)));
				} else if (key.equals(Constant.powInforStr[1])) {
					// 非外键
					pow.setRatedPower(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.powInforStr[2])) {
					pow.setPowerIn(list.get(0));
				} else if (key.equals(Constant.powInforStr[3])) {
					pow.setOut3(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.powInforStr[4])) {
					pow.setOut5(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.powInforStr[5])) {
					pow.setOut5SB(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.powInforStr[6])) {
					pow.setOut121(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.powInforStr[7])) {
					pow.setOut122(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.powInforStr[8])) {
					pow.setOut12M(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.powInforStr[9])) {
					pow.setAttachment(list.get(0));
				} else if (key.equals(Constant.powComboBoxStr[0])) {
					// 一对多
					powIntf = list;
				} else {
					// 通用属性
					Changer.setUniAttribute(pow, key, list);
				}

			}
			// 默认属性
			Changer.setDefaultAttribute(pow);
			pow.setState(getValueByID(Constant.HW_DEFAULT_STATE));
			
			
//			Transaction t = sess.beginTransaction();
			sess.save(pow);
			// 电源接口表POW_Interface
			for (int j = 0; j < powIntf.size(); j++) {
				String s = powIntf.get(j);
				int num = s.toCharArray()[0];
				String intfName = s.substring(2);
				Interface intf = getInterfaceByName(intfName);

				POW_Interface pi = new POW_Interface();
				pi.setPow(pow);
				pi.setIntf_id(intf);
				pi.setNum(num);
				sess.save(pi);
			}
			sess.flush();
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private List<String> getValueByKey(int i, int j) {
		List<String> rl = new ArrayList<String>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value where KEY>=" + i + " AND KEY<=" + j+" ORDER BY VALUE";
		List<Value> l = sess.createQuery(sql).list();
		for (int k = 0; k < l.size(); k++) {
			rl.add(l.get(k).getValue());
		}
		
		return rl;
	}

	private Value getValueByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value WHERE VALUE='" + name + "'";
		List<Value> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	private Interface getInterfaceByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Interface WHERE NAME='" + name + "'";
		List<Interface> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	private List<Item> getFilterPriceItem() {
		List<Item> choices = new ArrayList<Item>();
		for (int i = 0; i < (Constant.NETWORKADAPTER_PRICE.length); i++) {
			choices.add(Constant.NETWORKADAPTER_PRICE[i]);
		}
		return choices;
	}

	private List<Item> getFilterBrandItem() {
		Session sess = sessionFactory.getCurrentSession();

		String sql = "FROM BRD_POW";
		List<BRD_POW> brandList = sess.createQuery(sql).list();
		List<Item> choices = new ArrayList<Item>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(new Item(brandList.get(i).getBrand(), brandList.get(i)
					.getId()));
		}

		
		return choices;
	}

	private Value getValueByID(Integer integer) {
		Session sess = sessionFactory.getCurrentSession();
		Value c = (Value) sess.load(Value.class, integer);
		
		return c;
	}

	private Interface getInterfaceByID(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Interface c = (Interface) sess.load(Interface.class, id);
		
		return c;

	}

	private BRD_POW getBrandById(Integer integer) {
		Session sess = sessionFactory.getCurrentSession();
		BRD_POW c = (BRD_POW) sess.load(BRD_POW.class, integer);
		
		return c;
	}

	@Override
	public void save(Power t) {
		Session sess = sessionFactory.getCurrentSession();
//		Transaction tran = sess.beginTransaction();
		sess.saveOrUpdate(t);
		sess.flush();
		
	}

	@Override
	public int filterPageNum(Map<String, List<Integer>> map, HardwareSet hs) {

		List<Power> hwList = null;
		List<Power> matchList = new ArrayList<Power>();

		String sql = "FROM Power WHERE STATE=882";
		Session sess = sessionFactory.getCurrentSession();
		hwList = sess.createQuery(sql).list();

		hwList = uniFilter(hs, hwList);

		if (map==null || map.size()==0)
			return hwList.size();
		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.powItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.powItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer
								.checkPrice(hwList.get(j),
										Constant.POWER_PRICE[list.get(k) - 1]
												.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.powItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer
								.checkPrice(hwList.get(j),
										Constant.POWER_RATED[list.get(k) - 1]
												.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.powItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getFanSize().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<Power>();
			}
		}
		
		return matchList.size();

	}

	public int getListPageNum(HardwareSet hs) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Power WHERE STATE=882";
		List<Power> list = sess.createQuery(sql).list();
		if (hs != null) {
			list = HsFilter(hs, list);
		}
		
		return list.size();
	}

	private List<Power> uniFilter(HardwareSet hs, List<Power> list) {
		if (hs != null) {
			list = HsFilter(hs, list);
		}
		return list;
	}

	@Override
	public List<Power> getOneBySupplierID(int id) {
		// TODO Auto-generated method stub
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Power where SUPPLIER="+id;
		List<Power> list = sess.createQuery(sql).list();
		
		return list;
	}
}
